package com.leo.javase.thread.itcast;

public class TraditionalThreadSynchronized {

	public static void main(String[] args) {
		new TraditionalThreadSynchronized().init();
	}
	/**
	 * nleolian
	 * ieyi
	 */
	public void init(){
		final Outputer out = new Outputer();
		new Thread(new Runnable(){
			public void run() {
				while(true){
					/*try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}*/
					out.output1("leolian");
				}
			}
		}).start();
		new Thread(new Runnable(){
			public void run() {
				while(true){
					/*try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}*/
					out.output3("nieyi");
				}
			}
		}).start();
	}
	
	/**
	 * 内部类
	 * @author Lian
	 */
	static class Outputer{
		//String lock = "";
		
		public void output1(String name){
			int i = name.length();
			
			synchronized (Outputer.class) {
				for(int j=0; j<i; j++){
					System.out.print(name.charAt(j));
				}
				System.out.println();
			}
			
		}
		/**
		 * 同步方法
		 * @param name
		 */
		public synchronized void output2(String name){
			int i = name.length();
			for(int j=0; j<i; j++){
				System.out.print(name.charAt(j));
			}
			System.out.println();
		}
		
		/**
		 * 静态方法
		 * @param name
		 */
		public static synchronized void output3(String name){
			int i = name.length();
			for(int j=0; j<i; j++){
				System.out.print(name.charAt(j));
			}
			System.out.println();
		}
	}
	
}
